﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Landscape;
using Landscape.Members;

namespace Landscape.Web.Controls {
    public class WidgetParam {
        public string Name {
            get;
            set;
        }

        public string value {
            get;
            set;
        }
    }

    public class WidgetParameter {
        private string _key;
        public string key {
            get {
                return _key;
            }
            set{
                if (string.IsNullOrEmpty(value)) {
                    throw new Exception("Parameter key cannot be null or empty");
                }
                _key = value;
            }
        }
        public string value {
            get;
            set;
        }
    }

    [PersistChildren(true)]
    public class WidgetControl : System.Web.UI.WebControls.WebControl, INamingContainer {
        //this is supposed to get rid of the span tag
        protected override void Render(HtmlTextWriter writer) {
            this.RenderContents(writer);
        }  

        protected Literal litResult;
        /// <summary>
        /// The widget name used to retrieve its template from the source folders
        /// </summary>
        public string WidgetName {
            get;
            set;
        }

        private List<WidgetParameter> _Parameters = new List<WidgetParameter>();
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public List<WidgetParameter> Parameters {
            get {
                return _Parameters;
            }
        }


        /// <summary>
        /// A dictionary of optional parameters to supply
        /// </summary>
        public Dictionary<string, object> WidgetParams {
            get;
            set;
        }
        /// <summary>
        /// The data source used to be transformed
        /// </summary>
        public object DataSource {
            get;
            set;
        }

        protected override void OnInit(EventArgs e) {
            this.litResult = new Literal();
            this.Controls.Add(litResult);
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e) {
            if (!string.IsNullOrEmpty(this.WidgetName)) {
                if (this.WidgetParams == null) {
                    this.WidgetParams = new Dictionary<string, object>();
                }
                if (this.Parameters.Count > 0) {
                    foreach (WidgetParameter p in this.Parameters) {
                        if (!this.WidgetParams.ContainsKey(p.key)) {
                            this.WidgetParams.Add(p.key, p.value);
                        } else {
                            this.WidgetParams[p.key] = p.value;
                        }
                    }
                }

                if (this.DataSource == null) {
                    var doc = new System.Xml.XmlDocument();
                    doc.LoadXml("<root/>");
                    this.DataSource = doc;
                }
                litResult.Text = Renderer.RenderWidget(WidgetName, this.DataSource, "", WidgetParams); 
            }
            base.OnLoad(e);
        }
    }
}